$.texyla("setDefaults", {
	imageGalleryPath: "%texyla_base%/plugins/img-gallery/img-gallery.php",
	imageGalleryThumbsPath: "%texyla_base%/plugins/img-gallery/img-gallery-thumb.php?file=%var%",
	imageGalleryWindowDimensions: [370, 370]
});

$.texyla("initPlugin", function () {
	this.options.imageGalleryPath = this.expand(this.options.imageGalleryPath);
});

// tlačítko
$.texyla("addButton", "img_gallery", function () {
	var _this = this;

	var el = $('<div><p class="wait">' + this.lng.wait + '</p></div>');
	
	// zobrazit okno
	this.window({
		name: "imageGallery",
		el: el,
		title: this.lng.imgGalleryHeading
	});
	
	// nahrát obrázky
	$.getJSON(this.options.imageGalleryPath, {}, function (data) {
		var gallery = $('<div class="gallery"></div>').height(300);
		el.empty().append(gallery);
		
		// funkce na vložení obrázku
		function insImg(img) {
			return function () {
				_this.texy.img(
					img.src,
					img.alt,
					'*',
					false
				);
				
				el.dialog("close");
			}
		}
		
		// projít obrázky
		for (var i = 0; i < data.images.length; i++) {
			var img = data.images[i];
			var thumbSrc = _this.expand(
				_this.options.imageGalleryThumbsPath,
				img.thumbSrcVar
			);
			var div = $(
				'<div class="gallery-item"><div class="image">' +
				'<img src="' + thumbSrc + '" width="' + img.thumbWidth + '" height="' + img.thumbHeight + '">' +
				'</div><div class="label">' +
				'<strong>' + img.name + '</strong><br>' +
				'<span class="alt">' + img.alt + '</span>' +
				'</div></div>'
			).click(insImg(img));
			
			gallery.append(div);
		}
	});
});
